<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="角色名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加</el-button>
    </div>
    <el-table :data="allRoles" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="编号" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色名" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="Actions"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row,$index}">
          <el-button
            class="mgr"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handleUpdate(row)"
          />
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(row, $index)" />
          <!-- <el-popconfirm
                        title="是否要删除此用户?"
                        confirm-button-text="确定"
                        cancel-button-text="取消"
                        @click="handleDelete(row, $index)"
                    >
                        <template #reference>
                            <el-button type="danger" icon="el-icon-delete" circle></el-button>
                        </template>
          </el-popconfirm>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- <pagination v-show="total > 0" @pagination="initData" /> -->
    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="角色名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="temp.description" />
        </el-form-item>
        <el-form-item label="权限">
          <el-tree
            ref="perm_tree"
            :data="allPerms"
            show-checkbox
            node-key="id"
            highlight-current
            :props="defaultProps"
          />
        </el-form-item>
        <el-form-item label="菜单">
          <el-tree
            ref="menu_tree"
            :data="allMenus"
            show-checkbox
            node-key="id"
            highlight-current
            :props="defaultProps"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? createData() : updateData()"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAllRoles, deleteRole, getAllPerms, getAllMenus } from '@/api/rbac'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
// import Pagination from '@/components/Pagination/index.vue' // secondary package based on el-pagination
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      allRoles: null,
      allPerms: null,
      allMenus: null,
      currentRole: null,
      titleMap: {
        update: '更新',
        create: '创建'
      },
      dialogFormVisible: false,
      dialogStatus: null,
      temp: {
        id: undefined,
        name: '',
        description: '',
        status: ''
      },
      listQuery: {
        title: ''
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    /**
         * 初始化数据
         */
    initData() {
      getAllRoles().then(res => {
        this.allRoles = res.data
      })
      getAllPerms().then(res => {
        this.allPerms = this.getPreData(res.data)
      })
      getAllMenus().then(res => {
        this.allMenus = this.getPreData(res.data)
      })
    },

    /**
         * 打开更新窗口
         * @param {*} row
         */
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    /**
         * 创建新数据
         */
    createData() {
      console.log(this.$refs.perm_tree.getCheckedKeys())
      console.log(this.$refs.menu_tree.getCheckedKeys())
    },
    /**
         * 更新数据
         */
    updateData() {
      console.log('update')
    },
    /**
         * 初始化temp
         */
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        description: '',
        status: ''
      }
    },
    /**
         * 打开创建窗口
         */
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    /**
         * 删除
         * @param {*} row
         * @param {*} index
         */
    handleDelete(row, index) {
      deleteRole(row.id).then(res => {
        console.log(res.data)
      })
      this.$notify({
        title: '消息',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      this.allRoles.splice(index, 1)
    },
    handleFilter() {

    },
    getPreData(list) {
      const map = {}; let node; const tree = []; let i
      for (i = 0; i < list.length; i++) {
        map[list[i].id] = list[i]
        list[i].children = []
      }
      for (i = 0; i < list.length; i += 1) {
        node = list[i]
        if (node.parent_id !== null) {
          map[node.parent_id].children.push(node)
        } else {
          tree.push(node)
        }
      }
      return tree
    }
  }
}
</script>

<style>
.mgr {
  margin-right: 20px;
}
</style>
